package com.jarchie.htgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jarchie.htgl.R;
import com.jarchie.htgl.base.BaseActivity;
import com.jarchie.htgl.constants.ConfigConstant;
import com.jarchie.htgl.utils.CommonUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by Jarchie on 2017\12\11.
 * 合同管理的欢迎页面，即LoadingPage
 */

public class SplashActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout layoutSkip;
    private TextView tvSplash, tvSkip;
    private Disposable mDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).fitsSystemWindows(false).statusBarColor(R.color.color_main).init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        layoutSkip = obtainView(R.id.layout_skip);
        tvSplash = obtainView(R.id.tv_splash);
        tvSkip = obtainView(R.id.tv_skip);
    }

    @Override
    protected void setListener() {
        layoutSkip.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        CommonUtils.setFont(this, tvSplash);
        //从0开始发射6个数字为：0-5依次输出，延时0s执行，每1s发射一次。
        mDisposable = Flowable.intervalRange(ConfigConstant.INTERVAL_START, ConfigConstant.INTERVAL_END, ConfigConstant.INTERVAL_DELAY, ConfigConstant.INTERVAL_REPEAT, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(@NonNull Long aLong) throws Exception {
                        //aLong是发射过来的数字，想要倒计时显示，用5-aLong
                        tvSkip.setText((ConfigConstant.INTERVAL_COMPUTATIONS - aLong) + " s");
                    }
                })
                .doOnComplete(new Action() { //发射数据完成时，执行跳转到登录页,并关闭当前页
                    @Override
                    public void run() throws Exception {
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        finish();
                    }
                })
                .subscribe();
    }

    //禁止返回键
    @Override
    public void onBackPressed() {}

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_skip:
                if (mDisposable != null) { //取消订阅，直接跳转登录页
                    mDisposable.dispose();
                }
                tvSkip.setText(ConfigConstant.INTERVAL_START + " s");
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
